package com.ruoyi.system.service.impl;

import com.aliyun.dingtalkcalendar_1_0.models.ListEventsHeaders;
import com.aliyun.dingtalkcalendar_1_0.models.ListEventsRequest;
import com.aliyun.dingtalkcalendar_1_0.models.ListEventsResponse;
import com.aliyun.dingtalkconference_1_0.models.*;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
import com.dingtalk.api.request.OapiV2UserListRequest;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.dingtalk.api.response.OapiV2UserListResponse;
import com.ruoyi.system.service.IMeetService;

import com.aliyun.dingtalkconference_1_0.Client;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.stereotype.Service;

@Service
public class IMeetServiceImpl implements IMeetService {

    /**
     * 使用 Token 初始化账号Client
     *
     * @return Client
     * @throws Exception
     */
    public static Client createClient() throws Exception {
        Config config = new Config();
//        config.setAccessKeyId("dingc4xzf67rpxeetum7");
//        config.setAccessKeySecret("KdQKouajSXoFp00nq3w2eWbtRO5r3CB2Zn89xffo7E32j1tVi9KocBTpYqvWijxp");
        config.protocol = "https";
        config.regionId = "central";
        return new Client(config);
    }


    @Override
    public QueryConferenceInfoResponse queryConferenceInfoWithOptions() throws Exception {
        Client client = createClient();
        QueryConferenceInfoHeaders queryConferenceInfoHeaders = new QueryConferenceInfoHeaders();
        String accessToken = getAccessToken();
        queryConferenceInfoHeaders.xAcsDingtalkAccessToken = accessToken;
        return client.queryConferenceInfoWithOptions("6323d7xxxxx", queryConferenceInfoHeaders, new RuntimeOptions());
    }

    @Override
    public QueryOrgConferenceListResponse queryOrgConferenceListWithOptions() throws Exception {
        Client client = createClient();
        QueryOrgConferenceListHeaders queryOrgConferenceListHeaders = new QueryOrgConferenceListHeaders();
        String accessToken = getAccessToken();
        queryOrgConferenceListHeaders.xAcsDingtalkAccessToken = accessToken;
        QueryOrgConferenceListRequest queryOrgConferenceListRequest = new QueryOrgConferenceListRequest()
                .setNextToken("0")
                .setMaxResults(10);
        return client.queryOrgConferenceListWithOptions(queryOrgConferenceListRequest, queryOrgConferenceListHeaders, new RuntimeOptions());
    }

    @Override
    public OapiV2DepartmentListsubResponse queryDeptList(Long deptId) throws Exception {

        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
        OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
        req.setDeptId(deptId == null ? 1L : deptId);
        OapiV2DepartmentListsubResponse rsp = client.execute(req, getAccessToken());
        return rsp;
    }

    @Override
    public OapiV2UserListResponse queryDeptUserList(Long deptId, Long cursor, Long size) throws Exception {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
        OapiV2UserListRequest req = new OapiV2UserListRequest();
        req.setDeptId(deptId);
        req.setCursor(cursor);
        req.setSize(size);
        OapiV2UserListResponse rsp = client.execute(req, getAccessToken());
        return rsp;
    }

    @Override
    public ListEventsResponse queryScheduleList(String unionId) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        com.aliyun.dingtalkcalendar_1_0.Client client = new com.aliyun.dingtalkcalendar_1_0.Client(config);
        ListEventsHeaders listEventsHeaders = new ListEventsHeaders();
        listEventsHeaders.xAcsDingtalkAccessToken = getAccessToken();
        ListEventsRequest listEventsRequest = new ListEventsRequest();
        ListEventsResponse response = client.listEventsWithOptions(unionId, "primary", listEventsRequest, listEventsHeaders, new RuntimeOptions());
        return response;
    }

    public static com.aliyun.dingtalkoauth2_1_0.Client create210Client() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkoauth2_1_0.Client(config);
    }

    public String getAccessToken() throws Exception {
        com.aliyun.dingtalkoauth2_1_0.Client client = create210Client();
        com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest()
                .setAppKey("dingc4xzf67rpxeetum7")
                .setAppSecret("KdQKouajSXoFp00nq3w2eWbtRO5r3CB2Zn89xffo7E32j1tVi9KocBTpYqvWijxp");
        GetAccessTokenResponse accessToken = client.getAccessToken(getAccessTokenRequest);
        return accessToken.getBody().getAccessToken();
    }

}
